package com.lt.integration.jpa.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应工具类
 * @author xiaobin
 *
 * @param <T>
 */
public class Response<T> {

	private static final Integer SUCCESS_CODE = 0;
	private static final Integer FAIL_CODE = 1;

	private static final String SUCCESS = "success";
	private static final String FAIL = "fail";

	int code;
	String message;
	T data;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public static Response<?> success() {
		return success(SUCCESS, "");
	}

	public static Response<?> success(String msg) {
		return success(msg, null);
	}

	public static <E> Response<E> success(E data) {
		return success(null, data);
	}

	public static <E> Response<E> success(String message, E data) {
		return response(SUCCESS_CODE, message, data);
	}
	public static Response<?> response(int code, String message) {
		return response(code, message, null);
	}
	public static <E> Response<E> response(int code, String message, E data) {
		Response<E> resp=new Response<E>();
		resp.setCode(code);
		resp.setMessage(message);
		resp.setData(data);
		return resp;
	}
	
	public static Response<?> fail() {
		return fail(FAIL);
	}
	
	public static Response<?> unauthorized() {
		return response(401, "unauthorized", null);
	}

	public static Response<?> fail(String msg) {
		return fail(msg, null);
	}

	public static <E> Response<E> fail(E data) {
		return fail(null, data);
	}
	
	public static <E> Response<E> fail(String message, E data) {
		return response(FAIL_CODE, message, data);
	}
	
	public static Response<Map<String, Object>> ok(){
		Map<String, Object> map = new HashMap<>();
		return success(map);
	}
	
	public Response<Map<String, Object>> put(String key, Object value){
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>)this.getData();
		map.put(key, value);
		return success(map);
	}
	

}
